//
// Created by steven on 2017/2/27.
// Copyright (c) 2017 steven. All rights reserved.
//

#import "GMSubUserAddViewController.h"
#import "GMSubUserDataController.h"

@interface GMSubUserAddViewController()

@property (nonatomic, strong)NSString *name;
@property (nonatomic, strong)NSString *phoneNumber;
@property (nonatomic, strong)GMSubUserDataController *subUserDataController;

@end

@implementation GMSubUserAddViewController {

}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.subUserDataController = [[GMSubUserDataController alloc] init];
    // content view
    [self setupContentView];
}

#pragma mark - setupContentView
- (void)setupContentView {
    self.navigationItem.title = @"添加子账户";
    // logo view
    [self.view addSubview:self.logoImageView];
    [self.logoImageView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:56];
    [self.logoImageView autoAlignAxisToSuperviewAxis:ALAxisVertical];
    [self.logoImageView autoSetDimensionsToSize:CGSizeMake(96, 96)];

    // password text field
    [self.view addSubview:self.phoneTextField];
    [self.phoneTextField autoAlignAxisToSuperviewAxis:ALAxisVertical];
    [self.phoneTextField autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.view withOffset:-AUTH_TEXT_FIELD_HEIGHT];
    [self.phoneTextField autoSetDimensionsToSize:CGSizeMake(self.view.frame.size.width*0.8,AUTH_TEXT_FIELD_HEIGHT)];
    // phone text field
    [self.view addSubview:self.nameTextField];
    [self.nameTextField autoPinEdge:ALEdgeLeading toEdge:ALEdgeLeading ofView:self.phoneTextField];
    [self.nameTextField autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:self.phoneTextField withOffset:-14];
    [self.nameTextField autoSetDimensionsToSize:CGSizeMake(self.view.frame.size.width*0.8, AUTH_TEXT_FIELD_HEIGHT)];

    // login button
    [self.view addSubview:self.loginButton];
    [self.loginButton autoAlignAxis:ALAxisVertical toSameAxisOfView:self.view];
    [self.loginButton autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.phoneTextField withOffset:14];
    [self.loginButton autoSetDimensionsToSize:CGSizeMake(self.view.frame.size.width*0.8, AUTH_TEXT_FIELD_HEIGHT)];
}

- (void)addAccountButtonPressed {
    self.name = self.nameTextField.text;
    self.phoneNumber = self.phoneTextField.text;

    if (self.name.length == 0) {
        [self showAlertMessage:@"请输入姓名"];
        return;;
    } else if(self.phoneNumber.length == 0) {
        [self showAlertMessage:@"请输入手机号"];
        return;;
    } else if(self.phoneNumber.length != 11) {
        [self showAlertMessage:@"请输入正确格式手机号"];
        return;;
    } else {
        NSDictionary *params = @{@"name":self.name, @"phone_number":self.phoneNumber};
        [self.subUserDataController createSubUserWithParams:params callback:^(NSString *error) {
            if (error == nil) {
                [self renderSuccessView];
            } else {
                [self showAlertMessage:error];
            }
        }];
    }
}

#pragma mark - render success view
- (void)renderSuccessView {
    [self showAlertMessage:@"子账号创建成功"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.navigationController popViewControllerAnimated:YES];
    });

}

#pragma mark - Getter for logoImageView
- (UIImageView *)logoImageView {
    if (!_logoImageView) {
        _logoImageView = [[UIImageView alloc] init];
        _logoImageView.image = [UIImage imageNamed:@"default-user-icon"];
    }

    return _logoImageView;
}

#pragma mark - Getter for phone text field
- (GMAuthTextField *)nameTextField {
    if (!_nameTextField) {
        _nameTextField = [[GMAuthTextField alloc] init];
        UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 16, 18)];
        leftImageView.image = [UIImage imageNamed:@"name-icon"];
        _nameTextField.leftView = leftImageView;
        _nameTextField.placeholder = @"姓名";
        _nameTextField.layer.borderColor = [UIColor clearColor].CGColor;
    }

    return _nameTextField;
}

#pragma mark - Getter for password text field
- (GMAuthTextField *)phoneTextField {
    if (!_phoneTextField) {
        _phoneTextField = [[GMAuthTextField alloc] init];
        UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 16, 18)];
        leftImageView.image = [UIImage imageNamed:@"phone-icon"];
        _phoneTextField.leftView = leftImageView;
        _phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
        _phoneTextField.placeholder = @"手机号";
        _phoneTextField.layer.borderColor = [UIColor clearColor].CGColor;
    }

    return _phoneTextField;
}

#pragma mark - Getter for login button
- (GMButton *)loginButton {
    if (!_loginButton) {
        _loginButton = [[GMButton alloc] init];
        _loginButton.titleLabel.font = [UIFont systemFontOfSize:AUTH_FONT_SIZE];
        [_loginButton setTitle:@"添加" forState:UIControlStateNormal];
        [_loginButton addTarget:self action:@selector(addAccountButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    }

    return _loginButton;
}
@end
